package com.tianyang.yukuaifu.common.view;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.tianyang.yukuaifu.R;

/**
 * Description : 等待弹窗旋转图标
 */
public class MoveRoundView extends ImageView {

    private float degree = 0f;

    private int time_step = 40;

    public MoveRoundView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setImageResource(R.drawable.common_bg_loading);
    }

    private Runnable refreshRunnable = new Runnable() {

        @Override
        public void run() {
            degree += 12;
            if (degree > 360) {
                degree = 0;
            }
            postInvalidate();
            postDelayed(this, time_step);

        }
    };

    public void start() {
        post(refreshRunnable);
    }

    public void stop() {
        removeCallbacks(refreshRunnable);
    }

    @Override
    protected void onDetachedFromWindow() {
        stop();
        super.onDetachedFromWindow();
    }

    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(degree, getWidth() / 2, getHeight() / 2);
        super.onDraw(canvas);
        canvas.restore();
    }
}
